// const commonUrl = "https://zzq.mynatapp.cc/pig/"; //公共路径 
// const commonUrl = "http://www.kfchzs.com/zsfs/mobile/"



// const commonUrl = "https://www.gczby.com/llsc/"
// const commonImgUrl = 'https://www.gczby.com/llsc/up/'
// const commonImgsrc = 'http://uhqosfz.nat.ipyingshe.com/disb/upload/'
// const commonpay = 'http://uhqosfz.nat.ipyingshe.com/disb/'
// const commonUrl = "http://uhqosfz.nat.ipyingshe.com/disb/"
// const commonImgUrl = 'http://uhqosfz.nat.ipyingshe.com/disb/up/'

// const commonUrl = "https://www.weisou.online/5GCPE/"
// const commonImgUrl = 'https://www.weisou.online/5GCPE/up/'
// const commonImgsrc = 'https://www.weisou.online/5GCPE/upload'
// const commonpay = 'https://www.weisou.online/5GCPE/'
// const commonUrl = "/web/"

const commonUrl = "http://localhost:8080/5GCPE/"
const commonImgUrl = 'http://localhost:8080/5GCPE/up/'
const commonImgsrc = 'http://localhost:8080/5GCPE/upload'
const commonpay = 'http://localhost:8080/5GCPE/'
// const commonUrl = "/api/"
// const commonImgUrl = '/api/'
// const commonImgsrc = '/api/upload/'
// const commonpay = '/api/'

// http://wwwp.cn.utools.club/zsfs/upload


// post请求封装
function postRequest(url, data) {

    // if(uni.getStorageSync('user_id')){   
    //     console.log(uni.getStorageSync('user_id'))
    // }else{
    //     window.localStorage.href = 'https://www.gczby.com/llsc/Wxlogin.do'
    // }
    uni.showLoading({
        title: '加载中',
        mask: false
    })
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        uni.request({
            url: commonUrl + url,
            data: postData,
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded",
                token: uni.getStorageSync("token")
            },
            success: function (res) {
                uni.hideLoading();
                // console.log(res.data,99)
                //返回什么就相应的做调整
                if (res.data.code == 200) {
                    resolve(res.data);
                } else if (res.data.code == -1) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                    uni.reLaunch({
                        url: '/pages/login/login'
                    })
                    // 请求服务器成功，但是由于服务器没有数据返回，此时无code。会导致这个空数据
                    //接口后面的then执行
                    // 不下去，导致报错，所以还是要resolve下，这样起码有个返回值，
                    //不会被阻断在那里执行不下去！
                    resolve(res.msg);
                } else if (res.data.code == '000') {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                } else {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            },
            error: function (e) {
                uni.hideLoading();
                reject("网络出错");
            }
        });
    });
    return promise;
}

// get请求封装
function getRequest(url, data) {
    // if(uni.getStorageSync('user_id')){
    //     console.log(uni.getStorageSync('user_id'))
    // }else{
    //     var p = window.location.protocol;
    //     var a = document.createElement("a");
    //     a.setAttribute("href", `https://www.gczby.com/llsc/Wxlogin.do`);
    //     a.setAttribute("target", "_blank");
    //     a.click();
    //     document.getElementsByTagName("body")[0].appendChild(a);
    //         }
    uni.showLoading({
        title: '加载中',
        mask: false
    })
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;

        uni.request({
            url: commonUrl + url,
            data: postData,
            method: "GET",
            dataType: "json",
            header: {
                "content-type": "application/json",
                token: uni.getStorageSync("token")
            },
            success: function (res) {
                // console.log(res,999)
                uni.hideLoading();
                if (res.data.code == 200) {
                    resolve(res.data);
                } else if (res.data.code == -1) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                    uni.reLaunch({
                        url: '/'
                    })
                    // 请求服务器成功，但是由于服务器没有数据返回，此时无code。会导致这个空数据
                    //接口后面的then执行
                    // 不下去，导致报错，所以还是要resolve下，这样起码有个返回值，
                    //不会被阻断在那里执行不下去！
                    resolve(res.msg);
                } else if (res.data.code == '000') {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                } else {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            },
            error: function (e) {
                uni.hideLoading();
                reject("网络出错");
            }
        });
    });
    return promise;
}

module.exports = {
    post: postRequest,
    get: getRequest,
    imgUrl: commonImgUrl,
    commonUrl: commonUrl,
    imgSrc: commonImgsrc,
    commonpay: commonpay,

};